<template>
  <el-drawer size="80%" :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="110px"
      v-loading="formLoading"
    >
      <el-form-item label="交接单号" prop="handoverNumber">
        <el-input v-model="formData.handoverNumber" placeholder="保存后自动生成" disabled />
      </el-form-item>
      <el-form-item label="交接日期" prop="createDate">
        <el-date-picker
          v-model="formData.createDate"
          type="date"
          value-format="x"
          placeholder="选择交接日期"
        />
      </el-form-item>
      <el-form-item label="移交公司" prop="transferCompany">
        <el-input v-model="formData.transferCompany" placeholder="请输入移交公司" />
      </el-form-item>
      <el-form-item label="移交人" prop="transferPerson">
        <el-input v-model="formData.transferPerson" placeholder="请输入接收人" />
      </el-form-item>

      <el-form-item label="接收公司" prop="receivingCompany">
        <el-input v-model="formData.receivingCompany" placeholder="请输入接收公司" />
      </el-form-item>

      <el-form-item label="接收人" prop="receiverPerson">
        <el-input v-model="formData.receiverPerson" placeholder="请输入接收人" />
      </el-form-item>

      <el-form-item label="交接方式" prop="handoverMethod">
        <el-input v-model="formData.handoverMethod" placeholder="请输入交接方式" />
      </el-form-item>

      <el-form-item label="交接原因及备注" prop="reason">

        <el-input
          v-model="formData.reason"
          style="width: 100%;"
          :rows="4"
          type="textarea"
          placeholder="请输入原因及备注"
        />
      </el-form-item>


      <el-form-item label="交接材料明细" prop="materialsDetail">

        <el-input
          v-model="formData.materialsDetail"
          style="width: 100%;"
          :rows="4"
          type="textarea"
          placeholder="请输入交接材料明细"
        />
      </el-form-item>
      <el-form-item label="交接附件" >
        <OaFileManager :folderDisabled="true" :initial-file-ids="formData.files" @fileSelectSuccess="fileSelectScccess"  />
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <!-- <el-tabs v-model="subTabsName">
      <el-tab-pane label="交接附件" name="handoverAttachment">
        <HandoverAttachmentForm ref="handoverAttachmentFormRef" :handover-id="formData.id" />
      </el-tab-pane>
    </el-tabs> -->
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { HandoverManagementApi, HandoverManagementVO } from '@/api/oa/handovermanagement'

/** 交接管理 表单 */
defineOptions({ name: 'HandoverManagementForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  handoverNumber: undefined,
  transferCompany: undefined,
  transferPerson: undefined,
  receivingCompany: undefined,
  receiverPerson: undefined,
  handoverMethod: undefined,
  createDate: undefined,
  reason: undefined,
  materialsDetail: undefined,
  files: '',
  createdAt: undefined,
  updatedAt: undefined
})
const formRules = reactive({
  transferCompany: [{ required: true, message: '移交公司不能为空', trigger: 'blur' }],
  receivingCompany: [{ required: true, message: '接收公司不能为空', trigger: 'blur' }],
  handoverMethod: [{ required: true, message: '交接方式不能为空', trigger: 'blur' }],
  createDate: [{ required: true, message: '创建日期不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref




/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await HandoverManagementApi.getHandoverManagement(id)
    } finally {
      formLoading.value = false
    }
  }

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const fileSelectScccess = (fileIds: string[]) => {
  formData.value.files = fileIds.join(',')
}
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  // try {
  //   await handoverAttachmentFormRef.value.validate()
  // } catch (e) {
  //   subTabsName.value = 'handoverAttachment'
  //   return
  // }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as HandoverManagementVO
    // 拼接子表的数据
    // data.handoverAttachment = handoverAttachmentFormRef.value.getData()
    if (formType.value === 'create') {
      await HandoverManagementApi.createHandoverManagement(data)
      message.success(t('common.createSuccess'))
    } else {
      await HandoverManagementApi.updateHandoverManagement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    handoverNumber: undefined,
    transferCompany: undefined,
    transferPerson: undefined,
    receivingCompany: undefined,
    receiverPerson: undefined,
    handoverMethod: undefined,
    createDate: undefined,
    reason: undefined,
    materialsDetail: undefined,
    files: '',
    createdAt: undefined,
    updatedAt: undefined
  }
  formRef.value?.resetFields()
}
</script>
